﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using WPFExcelReport;
using MahApps.Metro;
using MahApps.Metro.Controls;
using System.Windows;
using System.Windows.Controls;
namespace ReflectionStudio.Classes
{
    public class DocumentFactory
    {
        #region ----------------SINGLETON----------------
        /// <summary>
        /// Singleton
        /// </summary>
        public static readonly DocumentFactory Instance = new DocumentFactory();
        public int RunID = 0;
        /// <summary>
        /// Private constructor for singleton pattern
        /// </summary>
        public DocumentFactory()
        {
            Documents = new List<DocumentBase>();
        }
        #endregion

        #region ---------------------PRIVATE---------------------

        public List<DocumentBase> Documents = null;
        public System.Windows.Controls.TabControl MainTab;
        #endregion

        public bool IsSingleRun = false;

        #region ----------------METHODS----------------

        public void Initialize(TabControl _MainTab)
        {
            MainTab = _MainTab;
        }
        public Grid MainContent;
        #endregion
        void RemoveDoc(string docname)
        {
            int idx = 0;
            bool isfound = false;
            foreach (DocumentBase doc in DocumentFactory.Instance.Documents)
            {

                if (doc.Tag.ToString().Trim() == docname.Trim())
                {
                    isfound = true;
                    break;
                }
                idx++;
            }
            if (isfound)
            {
                Documents.RemoveAt(idx);
            }
        }
        public MetroTabItem OpenDocument(Type docType, Dictionary<string, object> dicParamater, ref DocumentBase doc)
        {
            string DEFAULTUI = "";
            if (dicParamater.Keys.Contains("DEFAULTUI"))
            {
                DEFAULTUI = dicParamater["DEFAULTUI"].ToString();
            }
            string tagname = docType.Assembly.GetName().Name.Trim() + DEFAULTUI;
            MetroTabItem tb = null;
            doc = FindDocument(docType, DEFAULTUI);
            tb = FindTab(tagname);
            if (tb != null)
                return tb;
            if (tb == null)//移除已经关闭的
            {
                if (doc != null)
                {
                    RemoveDoc(doc.Tag.ToString());
                    doc = null;
                }
            }
            if (doc == null)
            {
                try
                {
                    doc = (DocumentBase)Activator.CreateInstance(docType);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ex.StackTrace, ex);
                }
                if (doc == null)
                    return null;
                /*doc.ShowInTaskbar = true;
                doc.UseNoneWindowStyle = true;
                doc.AllowsTransparency = true;
                //doc.Show();
                
                //return tb;*/
                doc.Tag = docType.Assembly.GetName().Name.Trim() + DEFAULTUI;
                doc.RUNID = doc.Tag.ToString().Trim() + RunID;
                Documents.Add(doc);
                doc.SetUp(dicParamater);
            }

            string name = "";
            if (IsSingleRun)
            {
                MainContent.Children.Add(doc);
            }
            else
            {


                if (doc != null)
                {
                    name = "tb" + doc.Name;
                    tb = (MetroTabItem)MainTab.FindName(name);
                    if (tb == null)
                    {
                        tb = new MetroTabItem();
                    }
                    tb.CloseButtonEnabled = true;
                    tb.Header = doc.Name;
                    name = "tb" + docType.Name.ToString();
                    tb.Tag = tagname;
                    /*StackPanel stk = new StackPanel();
                    stk.Children.Add(doc);
                    tb.Content = stk;*/
                    tb.Content = doc;
                    MainTab.Items.Add(tb);
                }
            }
            return tb;
        }

        private DocumentBase FindDocument(Type docType, string DEFAULTUI)
        {
            IEnumerable<DocumentBase> list = Documents.Where(d => d.Tag.ToString() == docType.Assembly.GetName().Name.Trim() + DEFAULTUI);
            if (list.Count() == 1)
                return list.First() as DocumentBase;
            else
            {
                return null;
            }
        }
        private MetroTabItem FindTab(string tagname)
        {
            MetroTabItem tb = null;
            foreach (MetroTabItem tab in MainTab.Items)
            {
                if (tab.Tag.ToString() == tagname)
                {
                    tb = tab;
                    break;
                }
            }
            return tb;
        }


    }
}
